<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of m_bieumau
 *
 * @author Trung Đặng
 */
class M_bieumau extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function getID() {
        $this->db->select_max('Ma_BieuMau');
        $query = $this->db->get('tbl_bieumau');
        $data = $query->row_array();
        $id = $data['Ma_BieuMau'];

        if (strlen($id) == 0) {
            $id = 'BM0000';
        }
        $id++;

        return $id;
    }

    public function getAll($F) {
        if($F!=100){
        $this->db->where('F', $F);}
        $query = $this->db->get('tbl_bieumau');
        $dsbieumau = FALSE;
        foreach ($query->result_array() as $row) {

            $dsbieumau[] = $row;
        }
        return $dsbieumau;
    }
    public function getAllMa() {
       
       
        $query = $this->db->get('tbl_bieumau');
        $dsbieumau=FALSE;
        foreach ($query->result_array() as $row) {

            $dsbieumau[$row['Ma_BieuMau']] = $row['TenBieuMau'];
        }
        return $dsbieumau;
    }

    public function getOneID($madon) {
        $this->db->where('Ma_BieuMau', $madon);
        $query = $this->db->get('tbl_bieumau');

        return $query->row_array();
    }

    function getODuLieuId($Ma_BieuMau) {
        $this->db->where('FK_Ma_BieuMau', $Ma_BieuMau);
        $query = $this->db->get('tbl_odulieu');
        return $query->result_array();
    }

    public function them($File, $Ma_BieuMau, $TenBieuMau, $MoTa, $Loai) {

        $TenFile = common::UpLoadFile($File, $Ma_BieuMau);
        
        if ($TenFile != FALSE) {
            $this->db->set('Ma_BieuMau', $Ma_BieuMau);
            $this->db->set('TenBieuMau', $TenBieuMau);
            $this->db->set('TenFile', $TenFile);
            $this->db->set('MoTa', $MoTa);
            $this->db->set('Loai', $Loai);
            $this->db->set('ThoiGian', date('Y-m-d H:i:s'));
            $this->db->insert('tbl_bieumau');
        }
    }

    function capnhat($Ma_BieuMau, $TenBieuMau, $MoTa, $Loai) {
        $this->db->where('Ma_BieuMau', $Ma_BieuMau);
        $this->db->set("TenBieuMau", $TenBieuMau);
        $this->db->set('MoTa', $MoTa);
        $this->db->set('Loai', $Loai);

        $this->db->update('tbl_bieumau');
    }

    function duyet($Ma_BieuMau, $F) {
        $this->db->where('Ma_BieuMau', $Ma_BieuMau);
        $this->db->set('F', $F);
        $this->db->update('tbl_bieumau');
    }

    function xoa($Ma_BieuMau) {

        $this->db->where('Ma_BieuMau', $Ma_BieuMau);
        $query = $this->db->get('tbl_bieumau');
        common::DeleteLoadFile($query->row_array()['TenFile']);
        $this->db->where('Ma_BieuMau', $Ma_BieuMau);
        $this->db->delete('tbl_bieumau');
        $this->db->where('FK_Ma_BieuMau', $Ma_BieuMau);
        $this->db->delete('tbl_odulieu');
    }

}
